package org.vectrics.common.view;

import org.apache.ecs.html.Html;
import org.apache.ecs.html.Head;
import org.apache.ecs.html.Body;
import org.apache.ecs.html.Title;
import org.apache.ecs.Element;
import org.apache.ecs.html.Link;
import org.apache.log4j.Category;
import org.vectrics.common.html.*;

/**
 * @author Mike Moore
 * @version 1.0
 */

public class StandardPage extends Html {
    private static Category log = Category.getInstance(StandardPage.class);
    private static final String HTML_CONTENT_TYPE = "text/html";
    private Title title = new Title();
    private Head head = null;
    private Body body = null;
    private Table mainTable = null;
    private Row bannerRow = null;
    private Cell bannerCell = null;
    private Cell border1Cell = null;
    private Cell border2Cell = null;
    private Cell border3Cell = null;
    private Cell leftCell = null;
    private Cell rightCell = null;
    private Cell mainCell = null;
    private Row mainRow = null;
    private Row footerRow = null;
    private Cell footerCell = null;
    private Cell menuCell = null;
    //private boolean prettyPrint = false;

    public static String BANNER_ELEMENT = "banner";
    public static String LEFT_CELL_ELEMENT = "left_cell";
    public static String RIGHT_CELL_ELEMENT = "right_cell";
    public static String MAIN_CELL_ELEMENT = "main_cell";
    public static String FOOTER_CELL_ELEMENT = "footer_cell";
    private String graphicsDirectory = null;
    private Element titleStr = null;
    private static String TITLE_STRING_ID = "title_str";

    public StandardPage() {
        super();
    }

    public String getContentType() {
        return(HTML_CONTENT_TYPE);
    }

    protected Row makeMenuRow() {
        return(null);
    }

    protected void fillBannerCell(Cell bannerCell) {
    }

    public void init() {
        head = new Head();
        this.addElement(head);
        Element titleStr = title.addElement(TITLE_STRING_ID, "Vectrics");
        head.addElement(title);


        body = new Body();
        body.addAttribute("leftmargin", "0");
        body.addAttribute("topmargin", "0");
        //body.setPrettyPrint(true);
        this.addElement(body);

        mainTable = new Table();
        mainTable.setWidth(800);
        mainTable.setBorder(0);
        mainTable.setCellSpacing(0);
        mainTable.setCellPadding(0);
        body.addElement(mainTable);

        bannerRow = mainTable.addRow();
        bannerRow.setID("banner_row");
        bannerCell = bannerRow.addCell();
        bannerCell.setID("banner_cell");
        fillBannerCell(bannerCell);

        Row menuRow = makeMenuRow();

        if (menuRow != null) {
            mainTable.addElement(menuRow);
            //menuCell = menuRow.addCell();
        }


        Row contentRow = mainTable.addRow();
        contentRow.setID("content_row");
        Cell contentCell = contentRow.addCell();
        Table contentTable = new Table();
        contentCell.addElement(contentTable);
        contentTable.setWidth("100%");
        contentTable.setCellSpacing(0);
        contentTable.setCellPadding(0);
        Row subRow = contentTable.addRow();

        leftCell = subRow.addCell();
        leftCell.setID("left_cell");
        leftCell.setWidth("0%");
        border1Cell = subRow.addCell();
        border1Cell.setID("border1_cell");

        mainCell = subRow.addCell();
        mainCell.setID("main_cell");
        mainCell.setWidth("0%");
        border2Cell = subRow.addCell();
        border2Cell.setID("border2_cell");

        rightCell = subRow.addCell();
        rightCell.setID("right_cell");
        rightCell.setWidth("0%");

        border3Cell = subRow.addCell();
        border3Cell.setID("border3_cell");

        footerRow = mainTable.addRow();
        footerCell = footerRow.addCell();
        footerCell.setColSpan(6);
        afterInit();
    }
/*
    protected Cell createBannerCell(Row row) {
        Cell cell = bannerRow.addCell();
        cell.setId("std_banner_cell");
        cell.setColSpan(3);
        return(cell);
    }
*/

    protected void afterInit() {
    }

    public Cell findElement(String elementId) {
        Cell foundCell = null;
        if (elementId.compareTo(StandardPage.BANNER_ELEMENT) == 0)
            foundCell = bannerCell;
        else if (elementId.compareTo(StandardPage.LEFT_CELL_ELEMENT) == 0)
            foundCell = leftCell;
        else if (elementId.compareTo(StandardPage.RIGHT_CELL_ELEMENT) == 0)
            foundCell = rightCell;
        else if (elementId.compareTo(StandardPage.FOOTER_CELL_ELEMENT) == 0)
            foundCell = footerCell;
        else if (elementId.compareTo(StandardPage.MAIN_CELL_ELEMENT) == 0)
            foundCell = mainCell;

        if (foundCell == null) {
            log.warn("getElement() - could not find page elment: " + elementId);
        }
        return(foundCell);
    }

    public void setStyleSheet(String styleSheet)
    {
        Link link = new Link();
        link.setRel("stylesheet");
        link.setType("text/css");
        link.setHref(styleSheet);
        head.addElement(link);
    };

    public Body getBody() {
        return(body);
    }

    public Cell getBannerCell() {
        return bannerCell;
    }

//    public Row getBannerRow() {
//        return bannerRow;
//    }

    public Row getFooterRow() {
        return footerRow;
    }
    public Cell getLeftCell() {
        return leftCell;
    }
    public Cell getMainCell() {
        return mainCell;
    }
    public Cell getBorder1Cell() {
        return border1Cell;
    }
    public Cell getBorder2Cell() {
        return border2Cell;
    }
    public Cell getBorder3Cell() {
        return border3Cell;
    }
    public Row getMainRow() {
        return mainRow;
    }
    public Table getMainTable() {
        return mainTable;
    }
    public Cell getRightCell() {
        return rightCell;
    }
    public Cell getFooterCell() {
        return footerCell;
    }
    public Title getTitle() {
        return title;
    }

    public Head getHead() {
        return(head);
    }

    public Element setTitle(String titleString) {
        title.removeElement(TITLE_STRING_ID);
        titleStr = title.addElement(TITLE_STRING_ID, titleString);
        return(titleStr);
    }

    public void setGraphicsDirectory(String graphicsDirectory) {
        this.graphicsDirectory = graphicsDirectory;
    }
    public String getGraphicsDirectory() {
        return graphicsDirectory;
    }



}